﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Data.SqlServerCe;
using System.Data.SqlClient;
using System.Reflection;
using System.IO;

namespace MtgPrinter
{
    public class DatabaseHandler
    {/*
        protected SqlCeConnection db;
        #region Basic Methods
        public void createDBStructure()
        {
            // get a reference to the current assembly
            Assembly a = Assembly.GetExecutingAssembly();

            // get a list of resource names from the manifest
            string[] resNames = a.GetManifestResourceNames();
            foreach (String _s in resNames)
            {
                if (_s.EndsWith("mtg_proto.sdf", StringComparison.CurrentCultureIgnoreCase))
                {
                    using (Stream _dbstream = a.GetManifestResourceStream(_s))
                    {
                        using (Stream _out = File.Open("mtg.sdf", FileMode.CreateNew, FileAccess.Write))
                        {
                            byte[] buf = new byte[4096];
                            int howmany;
                            while ((howmany = _dbstream.Read(buf, 0, buf.Length)) != 0)
                            {
                                _out.Write(buf, 0, howmany);
                            }
                        }
                    }

                }
            }
            // runScript("db.sql");
        }

        void Connect()
        {
            String fname = "mtg.sdf";
            string pass = "magic";
            String ConnectionString = String.Format("DataSource=\"{0}\"; Password='{1}'", fname, pass);

            if (!File.Exists(fname))
            {
                //using (SqlCeEngine en = new SqlCeEngine(ConnectionString))
                //{
                //    en.CreateDatabase();
                //}
                createDBStructure();
            }
            Console.WriteLine(ConnectionString);
            db = new SqlCeConnection(ConnectionString);
            db.Open();
        }

        void Disconnect()
        {
            if (db != null)
            {
                db.Close();
                db.Dispose();
            }
            db = null;
        }

        private int executeNonQuery(String command)
        {
            using (SqlCeCommand myCommand = new SqlCeCommand())
            {
                myCommand.CommandText = command;
                myCommand.Connection = db;
                int affected = myCommand.ExecuteNonQuery();
                return affected;
            }
        }

        private SqlCeDataReader executeQuery(String query)
        {
            using (SqlCeCommand myCommand = new SqlCeCommand())
            {
                myCommand.CommandText = query;
                myCommand.Connection = db;
                SqlCeDataReader reader = myCommand.ExecuteReader();
                return reader;
            }
        }
        #endregion
        */
        
    }
}
